{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第2章 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "df = pd.read_csv('data/table.csv',index_col='ID')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、单级索引\n",
    "### 1. loc方法、iloc方法、[]操作符\n",
    "#### 最常用的索引方法可能就是这三类，其中iloc表示位置索引，loc表示标签索引，[]也具有很大的便利性，各有特点\n",
    "    注:iloc 位置索引(绝对数值索引),loc 标签索引(数字,字符混合),[]切片索引\n",
    "#### （a）loc方法\n",
    "#### ① 单行索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "School          S_1\n",
       "Class           C_1\n",
       "Gender            M\n",
       "Address    street_2\n",
       "Height          186\n",
       "Weight           82\n",
       "Math           87.2\n",
       "Physics          B+\n",
       "Name: 1103, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "df.loc[1103]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ② 多行索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "2304    S_2   C_3      F  street_6     164      81  95.5      A-"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2304</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>164</td>\n      <td>81</td>\n      <td>95.5</td>\n      <td>A-</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "df.loc[[1102,2304]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （注意：所有在loc中使用的切片全部包含右端点！这是因为如果作为Pandas的使用者，那么肯定不太关心最后一个标签再往后一位是什么，但是如果是左闭右开，那么就很麻烦，先要知道再后面一列的名字是什么，非常不方便，因此Pandas中将loc设计为左右全闭）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1304    S_1   C_3      M  street_2     195      70  85.2       A\n",
       "1305    S_1   C_3      F  street_5     187      69  61.7      B-\n",
       "2101    S_2   C_1      M  street_7     174      84  83.3       C\n",
       "2102    S_2   C_1      F  street_6     161      61  50.6      B+\n",
       "2103    S_2   C_1      M  street_4     157      61  52.5      B-"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1304</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>195</td>\n      <td>70</td>\n      <td>85.2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>1305</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>F</td>\n      <td>street_5</td>\n      <td>187</td>\n      <td>69</td>\n      <td>61.7</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>2101</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>174</td>\n      <td>84</td>\n      <td>83.3</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>2102</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>161</td>\n      <td>61</td>\n      <td>50.6</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2103</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>157</td>\n      <td>61</td>\n      <td>52.5</td>\n      <td>B-</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "df.loc[1304:2103].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "2402    S_2   C_4      M  street_7     166      82  48.7       B\n",
       "2401    S_2   C_4      F  street_2     192      62  45.3       A\n",
       "2305    S_2   C_3      M  street_4     187      73  48.9       B\n",
       "2304    S_2   C_3      F  street_6     164      81  95.5      A-\n",
       "2303    S_2   C_3      F  street_7     190      99  65.9       C"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2402</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>166</td>\n      <td>82</td>\n      <td>48.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2401</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>62</td>\n      <td>45.3</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>2305</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>187</td>\n      <td>73</td>\n      <td>48.9</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2304</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>164</td>\n      <td>81</td>\n      <td>95.5</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>2303</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>F</td>\n      <td>street_7</td>\n      <td>190</td>\n      <td>99</td>\n      <td>65.9</td>\n      <td>C</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "df.loc[2402::-1].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ③ 单列索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1101    173\n",
       "1102    192\n",
       "1103    186\n",
       "1104    167\n",
       "1105    159\n",
       "Name: Height, dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "df.loc[:,'Height'].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ④ 多列索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      Height  Math\n",
       "ID                \n",
       "1101     173  34.0\n",
       "1102     192  32.5\n",
       "1103     186  87.2\n",
       "1104     167  80.4\n",
       "1105     159  84.8"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Height</th>\n      <th>Math</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>173</td>\n      <td>34.0</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>192</td>\n      <td>32.5</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>186</td>\n      <td>87.2</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>167</td>\n      <td>80.4</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>159</td>\n      <td>84.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "df.loc[:,['Height','Math']].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      Height  Weight  Math\n",
       "ID                        \n",
       "1101     173      63  34.0\n",
       "1102     192      73  32.5\n",
       "1103     186      82  87.2\n",
       "1104     167      81  80.4\n",
       "1105     159      64  84.8"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "df.loc[:,'Height':'Math'].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ⑤ 联合索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      Height  Weight  Math\n",
       "ID                        \n",
       "1102     192      73  32.5\n",
       "1105     159      64  84.8\n",
       "1203     160      53  58.8\n",
       "1301     161      68  31.5\n",
       "1304     195      70  85.2"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1102</th>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>160</td>\n      <td>53</td>\n      <td>58.8</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n    </tr>\n    <tr>\n      <th>1304</th>\n      <td>195</td>\n      <td>70</td>\n      <td>85.2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "df.loc[1102:2401:3,'Height':'Math'].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ⑥ 函数式索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1201    S_1   C_2      M  street_5     188      68  97.0      A-\n",
       "1203    S_1   C_2      M  street_6     160      53  58.8      A+\n",
       "1301    S_1   C_3      M  street_4     161      68  31.5      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_5</td>\n      <td>188</td>\n      <td>68</td>\n      <td>97.0</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_6</td>\n      <td>160</td>\n      <td>53</td>\n      <td>58.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "df.loc[lambda x:x['Gender']=='M'].head()\n",
    "#loc中使用的函数，传入参数就是前面的df\n",
    "# df.loc[df['Gender']=='M'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "#这里的例子表示，loc中能够传入函数，并且函数的输入值是整张表，输出为标量、切片、合法列表（元素出现在索引中）、合法索引\n",
    "def f(x):\n",
    "    return [1101,1103]\n",
    "df.loc[f]"
   ]
  },
  {
   "source": [
    "#### ⑦ 布尔索引（将重点在第2节介绍）"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+\n",
       "1202    S_1   C_2      F  street_4     176      94  63.5      B-\n",
       "1301    S_1   C_3      M  street_4     161      68  31.5      B+\n",
       "1303    S_1   C_3      M  street_7     188      82  49.7       B\n",
       "2101    S_2   C_1      M  street_7     174      84  83.3       C"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>176</td>\n      <td>94</td>\n      <td>63.5</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1303</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>188</td>\n      <td>82</td>\n      <td>49.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2101</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>174</td>\n      <td>84</td>\n      <td>83.3</td>\n      <td>C</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "df.loc[df['Address'].isin(['street_7','street_4'])].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1101    False\n",
       "1102    False\n",
       "1103    False\n",
       "1104    False\n",
       "1105     True\n",
       "Name: Address, dtype: bool"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "df['Address'].isin(['street_7','street_4']).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+\n",
       "1202    S_1   C_2      F  street_4     176      94  63.5      B-\n",
       "1301    S_1   C_3      M  street_4     161      68  31.5      B+\n",
       "1303    S_1   C_3      M  street_7     188      82  49.7       B\n",
       "2101    S_2   C_1      M  street_7     174      84  83.3       C"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>176</td>\n      <td>94</td>\n      <td>63.5</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1303</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>188</td>\n      <td>82</td>\n      <td>49.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2101</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>174</td>\n      <td>84</td>\n      <td>83.3</td>\n      <td>C</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "df.loc[[True if i[-1]=='4' or i[-1]=='7' else False for i in df['Address'].values]].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小节：本质上说，loc中能传入的只有布尔列表和索引子集构成的列表，只要把握这个原则就很容易理解上面那些操作\n",
    "#### （b）iloc方法（注意与loc不同，切片右端点不包含）\n",
    "#### ① 单行索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "School          S_1\n",
       "Class           C_1\n",
       "Gender            F\n",
       "Address    street_2\n",
       "Height          167\n",
       "Weight           81\n",
       "Math           80.4\n",
       "Physics          B-\n",
       "Name: 1104, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "df.iloc[3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ② 多行索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "df.iloc[3:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ③ 单列索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1101    street_1\n",
       "1102    street_2\n",
       "1103    street_2\n",
       "1104    street_2\n",
       "1105    street_4\n",
       "Name: Address, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "df.iloc[:,3].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ④ 多列索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     Physics  Weight   Address Class\n",
       "ID                                  \n",
       "1101      A+      63  street_1   C_1\n",
       "1102      B+      73  street_2   C_1\n",
       "1103      B+      82  street_2   C_1\n",
       "1104      B-      81  street_2   C_1\n",
       "1105      B+      64  street_4   C_1"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Physics</th>\n      <th>Weight</th>\n      <th>Address</th>\n      <th>Class</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>A+</td>\n      <td>63</td>\n      <td>street_1</td>\n      <td>C_1</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>B+</td>\n      <td>73</td>\n      <td>street_2</td>\n      <td>C_1</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>B+</td>\n      <td>82</td>\n      <td>street_2</td>\n      <td>C_1</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>B-</td>\n      <td>81</td>\n      <td>street_2</td>\n      <td>C_1</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>B+</td>\n      <td>64</td>\n      <td>street_4</td>\n      <td>C_1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "df.iloc[:,7::-2].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ⑤ 混合索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     Physics  Weight   Address Class\n",
       "ID                                  \n",
       "1104      B-      81  street_2   C_1\n",
       "1203      A+      53  street_6   C_2\n",
       "1302      A-      57  street_1   C_3\n",
       "2101       C      84  street_7   C_1\n",
       "2105       A      81  street_4   C_1"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Physics</th>\n      <th>Weight</th>\n      <th>Address</th>\n      <th>Class</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1104</th>\n      <td>B-</td>\n      <td>81</td>\n      <td>street_2</td>\n      <td>C_1</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>A+</td>\n      <td>53</td>\n      <td>street_6</td>\n      <td>C_2</td>\n    </tr>\n    <tr>\n      <th>1302</th>\n      <td>A-</td>\n      <td>57</td>\n      <td>street_1</td>\n      <td>C_3</td>\n    </tr>\n    <tr>\n      <th>2101</th>\n      <td>C</td>\n      <td>84</td>\n      <td>street_7</td>\n      <td>C_1</td>\n    </tr>\n    <tr>\n      <th>2105</th>\n      <td>A</td>\n      <td>81</td>\n      <td>street_4</td>\n      <td>C_1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "source": [
    "df.iloc[3::4,7::-2].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ⑥ 函数式索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "df.iloc[lambda x:[3]].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小节：iloc中接收的参数只能为整数或整数列表或布尔列表，不能使用布尔Series，如果要用就必须如下把values拿出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "# df.iloc[df['School']=='S_1'].head() #报错\n",
    "df.iloc[(df['School']=='S_1').values].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c） []操作符\n",
    "#### （c.1）Series的[]操作\n",
    "#### ① 单元素索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "34.0"
      ]
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "s = pd.Series(df['Math'],index=df.index)\n",
    "s[1101]\n",
    "#使用的是索引标签"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ② 多行索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1101    34.0\n",
       "1102    32.5\n",
       "1103    87.2\n",
       "1104    80.4\n",
       "Name: Math, dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "s[0:4]\n",
    "#使用的是绝对位置的整数切片，与元素无关，这里容易混淆"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ③ 函数式索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "2102    50.6\n",
       "1301    31.5\n",
       "1105    84.8\n",
       "Name: Math, dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "s[lambda x: x.index[16::-6]]\n",
    "#注意使用lambda函数时，直接切片(如：s[lambda x: 16::-6])就报错，此时使用的不是绝对位置切片，而是元素切片，非常易错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ④ 布尔索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1103    87.2\n",
       "1104    80.4\n",
       "1105    84.8\n",
       "1201    97.0\n",
       "1302    87.7\n",
       "1304    85.2\n",
       "2101    83.3\n",
       "2205    85.4\n",
       "2304    95.5\n",
       "Name: Math, dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "s[s>80]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 【注意】如果不想陷入困境，请不要在行索引为浮点时使用[]操作符，因为在Series中[]的浮点切片并不是进行位置比较，而是值比较，非常特殊"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1    1\n",
       "3    2\n",
       "5    3\n",
       "6    4\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "s_int = pd.Series([1,2,3,4],index=[1,3,5,6])\n",
    "s_float = pd.Series([1,2,3,4],index=[1.,3.,5.,6.])\n",
    "s_int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "5    3\n",
       "6    4\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "s_int[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1.0    1\n",
       "3.0    2\n",
       "5.0    3\n",
       "6.0    4\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "s_float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "3.0    2\n",
       "5.0    3\n",
       "6.0    4\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "source": [
    "#注意和s_int[2:]结果不一样了，因为2这里是元素而不是位置\n",
    "#切片 >=2 的元素\n",
    "s_float[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c.2）DataFrame的[]操作\n",
    "#### ① 单行索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "source": [
    "df[1:2]  # 位置切片,左闭右开\n",
    "#这里非常容易写成df['label']，会报错\n",
    "#同Series使用了绝对位置切片\n",
    "#如果想要获得某一个元素，可用如下get_loc方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "row = df.index.get_loc(1102)  # 将行索引转换成位置的值.返回是int类型.\n",
    "df[row:row+1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ② 多行索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "source": [
    "#用切片，如果是选取指定的某几行，推荐使用loc，否则很可能报错\n",
    "df[3:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ③ 单列索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1101    S_1\n",
       "1102    S_1\n",
       "1103    S_1\n",
       "1104    S_1\n",
       "1105    S_1\n",
       "Name: School, dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "df['School'].head()   #列索引使用字符."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ④ 多列索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School  Math\n",
       "ID               \n",
       "1101    S_1  34.0\n",
       "1102    S_1  32.5\n",
       "1103    S_1  87.2\n",
       "1104    S_1  80.4\n",
       "1105    S_1  84.8"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Math</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>34.0</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>32.5</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>87.2</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>80.4</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>84.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "source": [
    "df[['School','Math']].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ⑤函数式索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      Math Physics\n",
       "ID                \n",
       "1101  34.0      A+\n",
       "1102  32.5      B+\n",
       "1103  87.2      B+\n",
       "1104  80.4      B-\n",
       "1105  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 35
    }
   ],
   "source": [
    "df[lambda x:['Math','Physics']].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ⑥ 布尔索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+\n",
       "1202    S_1   C_2      F  street_4     176      94  63.5      B-\n",
       "1204    S_1   C_2      F  street_5     162      63  33.8       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>176</td>\n      <td>94</td>\n      <td>63.5</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1204</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_5</td>\n      <td>162</td>\n      <td>63</td>\n      <td>33.8</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "source": [
    "df[df['Gender']=='F'].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小节：一般来说，[]操作符常用于列选择或布尔选择，尽量避免行的选择\n",
    "### 2. 布尔索引\n",
    "#### （a）布尔符号：'&','|','~'：分别代表和and，或or，取反not"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "2401    S_2   C_4      F  street_2     192      62  45.3       A\n",
       "2404    S_2   C_4      F  street_2     160      84  67.7       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>2401</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>62</td>\n      <td>45.3</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>2404</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>160</td>\n      <td>84</td>\n      <td>67.7</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 37
    }
   ],
   "source": [
    "df[(df['Gender']=='F')&(df['Address']=='street_2')].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1201    S_1   C_2      M  street_5     188      68  97.0      A-\n",
       "1302    S_1   C_3      F  street_1     175      57  87.7      A-\n",
       "1303    S_1   C_3      M  street_7     188      82  49.7       B\n",
       "1304    S_1   C_3      M  street_2     195      70  85.2       A"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_5</td>\n      <td>188</td>\n      <td>68</td>\n      <td>97.0</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>1302</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>F</td>\n      <td>street_1</td>\n      <td>175</td>\n      <td>57</td>\n      <td>87.7</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>1303</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>188</td>\n      <td>82</td>\n      <td>49.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>1304</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>195</td>\n      <td>70</td>\n      <td>85.2</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "source": [
    "df[(df['Math']>85)|(df['Address']=='street_7')].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1202    S_1   C_2      F  street_4     176      94  63.5      B-\n",
       "1203    S_1   C_2      M  street_6     160      53  58.8      A+\n",
       "1204    S_1   C_2      F  street_5     162      63  33.8       B\n",
       "1205    S_1   C_2      F  street_6     167      63  68.4      B-"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>176</td>\n      <td>94</td>\n      <td>63.5</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_6</td>\n      <td>160</td>\n      <td>53</td>\n      <td>58.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1204</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_5</td>\n      <td>162</td>\n      <td>63</td>\n      <td>33.8</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>1205</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>167</td>\n      <td>63</td>\n      <td>68.4</td>\n      <td>B-</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 39
    }
   ],
   "source": [
    "df[~((df['Math']>75)|(df['Address']=='street_1'))].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### loc和[]中相应位置都能使用布尔列表选择："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     Physics\n",
       "ID          \n",
       "1103      B+\n",
       "1104      B-\n",
       "1105      B+\n",
       "1201      A-\n",
       "1202      B-"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1103</th>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>B-</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "source": [
    "df.loc[df['Math']>60,df.columns=='Physics'].head()\n",
    "#思考：为什么df.loc[df['Math']>60,(df[:8]['Address']=='street_6').values].head()得到和上述结果一样？values能去掉吗？\n",
    "#提示:df.loc(行,列)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     Physics\n",
       "ID          \n",
       "1103      B+\n",
       "1104      B-\n",
       "1105      B+\n",
       "1201      A-\n",
       "1202      B-"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1103</th>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>B-</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "source": [
    "df.loc[df['Math']>60,(df[:8]['Address']=='street_6').values].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b） isin方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "2105    S_2   C_1      M  street_4     170      81  34.2       A\n",
       "2203    S_2   C_2      M  street_4     155      91  73.8      A+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>2105</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>170</td>\n      <td>81</td>\n      <td>34.2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>2203</th>\n      <td>S_2</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>155</td>\n      <td>91</td>\n      <td>73.8</td>\n      <td>A+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 42
    }
   ],
   "source": [
    "df[df['Address'].isin(['street_1','street_4'])&df['Physics'].isin(['A','A+'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "2105    S_2   C_1      M  street_4     170      81  34.2       A\n",
       "2203    S_2   C_2      M  street_4     155      91  73.8      A+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>2105</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>170</td>\n      <td>81</td>\n      <td>34.2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>2203</th>\n      <td>S_2</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>155</td>\n      <td>91</td>\n      <td>73.8</td>\n      <td>A+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "source": [
    "#上面也可以用字典方式写：\n",
    "df[df[['Address','Physics']].isin({'Address':['street_1','street_4'],'Physics':['A','A+']}).all(1)]\n",
    "#all与&的思路是类似的，其中的1代表按照跨列方向判断是否全为True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 快速标量索引\n",
    "#### 当只需要取一个元素时，at和iat方法能够提供更快的实现："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/plain": "'S_1'"
     },
     "metadata": {}
    },
    {
     "output_type": "display_data",
     "data": {
      "text/plain": "'S_1'"
     },
     "metadata": {}
    },
    {
     "output_type": "display_data",
     "data": {
      "text/plain": "'S_1'"
     },
     "metadata": {}
    },
    {
     "output_type": "display_data",
     "data": {
      "text/plain": "'S_1'"
     },
     "metadata": {}
    }
   ],
   "source": [
    "display(df.at[1101,'School'])\n",
    "display(df.loc[1101,'School'])\n",
    "display(df.iat[0,0])\n",
    "display(df.iloc[0,0])\n",
    "#可尝试去掉注释对比时间\n",
    "#%timeit df.at[1101,'School']\n",
    "#%timeit df.loc[1101,'School']\n",
    "#%timeit df.iat[0,0]\n",
    "#%timeit df.iloc[0,0]"
   ]
  },
  {
   "source": [
    "### 4. 区间索引\n",
    "#### 此处介绍并不是说只能在单级索引中使用区间索引，只是作为一种特殊类型的索引方式，在此处先行介绍\n",
    "#### （a）利用interval_range方法"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "IntervalIndex([(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]]\n",
       "              closed='right',\n",
       "              dtype='interval[int64]')"
      ]
     },
     "metadata": {},
     "execution_count": 45
    }
   ],
   "source": [
    "pd.interval_range(start=0,end=5)\n",
    "#closed参数可选'left''right''both''neither'为闭端，默认左开右闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "IntervalIndex([(0, 5], (5, 10], (10, 15], (15, 20], (20, 25], (25, 30], (30, 35], (35, 40]]\n",
       "              closed='right',\n",
       "              dtype='interval[int64]')"
      ]
     },
     "metadata": {},
     "execution_count": 46
    }
   ],
   "source": [
    "pd.interval_range(start=0,periods=8,freq=5)\n",
    "#periods(周期)参数控制区间个数，freq(频率)控制步长"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）利用cut将数值列转为区间为元素的分类变量，例如统计数学成绩的区间情况："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1101      (0, 40]\n",
       "1102      (0, 40]\n",
       "1103    (80, 100]\n",
       "1104    (80, 100]\n",
       "1105    (80, 100]\n",
       "Name: Math, dtype: category\n",
       "Categories (4, interval[int64]): [(0, 40] < (40, 60] < (60, 80] < (80, 100]]"
      ]
     },
     "metadata": {},
     "execution_count": 47
    }
   ],
   "source": [
    "math_interval = pd.cut(df['Math'],bins=[0,40,60,80,100])\n",
    "#注意，如果没有类型转换，此时并不是区间类型，而是category类型\n",
    "math_interval.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c）区间索引的选取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                 ID  Math\n",
       "Math_interval            \n",
       "(0, 40]        1101  34.0\n",
       "(0, 40]        1102  32.5\n",
       "(80, 100]      1103  87.2\n",
       "(80, 100]      1104  80.4\n",
       "(80, 100]      1105  84.8"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ID</th>\n      <th>Math</th>\n    </tr>\n    <tr>\n      <th>Math_interval</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>(0, 40]</th>\n      <td>1101</td>\n      <td>34.0</td>\n    </tr>\n    <tr>\n      <th>(0, 40]</th>\n      <td>1102</td>\n      <td>32.5</td>\n    </tr>\n    <tr>\n      <th>(80, 100]</th>\n      <td>1103</td>\n      <td>87.2</td>\n    </tr>\n    <tr>\n      <th>(80, 100]</th>\n      <td>1104</td>\n      <td>80.4</td>\n    </tr>\n    <tr>\n      <th>(80, 100]</th>\n      <td>1105</td>\n      <td>84.8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 48
    }
   ],
   "source": [
    "df_i = df.join(math_interval,rsuffix='_interval')[['Math','Math_interval']]\\\n",
    "            .reset_index().set_index('Math_interval')\n",
    "df_i.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                 ID  Math\n",
       "Math_interval            \n",
       "(60, 80]       1202  63.5\n",
       "(60, 80]       1205  68.4\n",
       "(60, 80]       1305  61.7\n",
       "(60, 80]       2104  72.2\n",
       "(60, 80]       2202  68.5"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ID</th>\n      <th>Math</th>\n    </tr>\n    <tr>\n      <th>Math_interval</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>(60, 80]</th>\n      <td>1202</td>\n      <td>63.5</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>1205</td>\n      <td>68.4</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>1305</td>\n      <td>61.7</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>2104</td>\n      <td>72.2</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>2202</td>\n      <td>68.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 49
    }
   ],
   "source": [
    "df_i.loc[65].head()\n",
    "#包含该值就会被选中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                 ID  Math\n",
       "Math_interval            \n",
       "(60, 80]       1202  63.5\n",
       "(60, 80]       1205  68.4\n",
       "(60, 80]       1305  61.7\n",
       "(60, 80]       2104  72.2\n",
       "(60, 80]       2202  68.5"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ID</th>\n      <th>Math</th>\n    </tr>\n    <tr>\n      <th>Math_interval</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>(60, 80]</th>\n      <td>1202</td>\n      <td>63.5</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>1205</td>\n      <td>68.4</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>1305</td>\n      <td>61.7</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>2104</td>\n      <td>72.2</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>2202</td>\n      <td>68.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 52
    }
   ],
   "source": [
    "df_i.loc[[65,90]].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 如果想要选取某个区间，先要把分类变量转为区间变量，再使用overlap方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'IntervalIndex' object has no attribute 'overlaps'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-53-ff9172f32f21>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# df_i.loc[pd.Interval(70,75)].head()# 跨区间会报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdf_i\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mdf_i\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'interval'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moverlaps\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mInterval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m70\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m85\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 报错没有'overlaps'属性?\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'IntervalIndex' object has no attribute 'overlaps'"
     ]
    }
   ],
   "source": [
    "# df_i.loc[pd.Interval(70,75)].head()# 跨区间会报错\n",
    "df_i[df_i.index.astype('interval').overlaps(pd.Interval(70, 85))].head()  # 报错没有'overlaps'属性?"
   ]
  },
  {
   "source": [
    "#### overlaps https://blog.csdn.net/Datawhale/article/details/106416646"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、多级索引\n",
    "### 1. 创建多级索引\n",
    "#### （a）通过from_tuple或from_arrays\n",
    "#### ① 直接创建元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['A', 'B'], ['a', 'b']],\n",
       "           labels=[[0, 0, 1, 1], [0, 1, 0, 1]],\n",
       "           names=['Upper', 'Lower'])"
      ]
     },
     "metadata": {},
     "execution_count": 52
    }
   ],
   "source": [
    "tuples = [('A','a'),('A','b'),('B','a'),('B','b')]\n",
    "mul_index = pd.MultiIndex.from_tuples(tuples, names=('Upper', 'Lower'))\n",
    "mul_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               Score\n",
       "Upper Lower         \n",
       "A     a      perfect\n",
       "      b         good\n",
       "B     a         fair\n",
       "      b          bad"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>Score</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th>Lower</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">A</th>\n      <th>a</th>\n      <td>perfect</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>good</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">B</th>\n      <th>a</th>\n      <td>fair</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>bad</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 53
    }
   ],
   "source": [
    "pd.DataFrame({'Score':['perfect','good','fair','bad']},index=mul_index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ② 利用zip创建元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               Score\n",
       "Upper Lower         \n",
       "A     a      perfect\n",
       "      b         good\n",
       "B     a         fair\n",
       "      b          bad"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>Score</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th>Lower</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">A</th>\n      <th>a</th>\n      <td>perfect</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>good</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">B</th>\n      <th>a</th>\n      <td>fair</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>bad</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 53
    }
   ],
   "source": [
    "L1 = list('AABB')\n",
    "L2 = list('abab')\n",
    "tuples = list(zip(L1,L2))\n",
    "mul_index = pd.MultiIndex.from_tuples(tuples, names=('Upper', 'Lower'))\n",
    "pd.DataFrame({'Score':['perfect','good','fair','bad']},index=mul_index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ③ 通过Array创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               Score\n",
       "Upper Lower         \n",
       "A     a      perfect\n",
       "      b         good\n",
       "B     a         fair\n",
       "      b          bad"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>Score</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th>Lower</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">A</th>\n      <th>a</th>\n      <td>perfect</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>good</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">B</th>\n      <th>a</th>\n      <td>fair</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>bad</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 55
    }
   ],
   "source": [
    "arrays = [['A','a'],['A','b'],['B','a'],['B','b']]\n",
    "mul_index = pd.MultiIndex.from_tuples(arrays, names=('Upper', 'Lower'))\n",
    "pd.DataFrame({'Score':['perfect','good','fair','bad']},index=mul_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['A', 'B'], ['a', 'b']],\n",
       "           codes=[[0, 0, 1, 1], [0, 1, 0, 1]],\n",
       "           names=['Upper', 'Lower'])"
      ]
     },
     "metadata": {},
     "execution_count": 56
    }
   ],
   "source": [
    "mul_index\n",
    "#由此看出内部自动转成元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）通过from_product"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['A', 'B'], ['a', 'b']],\n",
       "           codes=[[0, 0, 1, 1], [0, 1, 0, 1]],\n",
       "           names=['Upper', 'Lower'])"
      ]
     },
     "metadata": {},
     "execution_count": 57
    }
   ],
   "source": [
    "L1 = ['A','B']\n",
    "L2 = ['a','b']\n",
    "pd.MultiIndex.from_product([L1,L2],names=('Upper', 'Lower'))\n",
    "#两两相乘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c）指定df中的列创建（set_index方法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               School Gender  Height  Weight  Math Physics\n",
       "Class Address                                             \n",
       "C_1   street_1    S_1      M     173      63  34.0      A+\n",
       "      street_2    S_1      F     192      73  32.5      B+\n",
       "      street_2    S_1      M     186      82  87.2      B+\n",
       "      street_2    S_1      F     167      81  80.4      B-\n",
       "      street_4    S_1      F     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">C_1</th>\n      <th>street_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 214
    }
   ],
   "source": [
    "df_using_mul = df.set_index(['Class','Address'])\n",
    "df_using_mul.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 多层索引切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               School Gender  Height  Weight  Math Physics\n",
       "Class Address                                             \n",
       "C_1   street_1    S_1      M     173      63  34.0      A+\n",
       "      street_2    S_1      F     192      73  32.5      B+\n",
       "      street_2    S_1      M     186      82  87.2      B+\n",
       "      street_2    S_1      F     167      81  80.4      B-\n",
       "      street_4    S_1      F     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">C_1</th>\n      <th>street_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 215
    }
   ],
   "source": [
    "df_using_mul.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （a）一般切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               School Gender  Height  Weight  Math Physics\n",
       "Class Address                                             \n",
       "C_2   street_5    S_1      M     188      68  97.0      A-\n",
       "      street_5    S_1      F     162      63  33.8       B\n",
       "      street_5    S_2      M     193     100  39.1       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">C_2</th>\n      <th>street_5</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>188</td>\n      <td>68</td>\n      <td>97.0</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>street_5</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>162</td>\n      <td>63</td>\n      <td>33.8</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>street_5</th>\n      <td>S_2</td>\n      <td>M</td>\n      <td>193</td>\n      <td>100</td>\n      <td>39.1</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 216
    }
   ],
   "source": [
    "#df_using_mul.loc['C_2','street_5']\n",
    "#当索引不排序时，单个索引会报出性能警告\n",
    "#df_using_mul.index.is_lexsorted()\n",
    "#该函数检查是否排序\n",
    "df_using_mul.sort_index().loc['C_2','street_5']\n",
    "#df_using_mul.sort_index().index.is_lexsorted()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                Height Gender\n",
       "Class Address                \n",
       "C_2   street_1     175      M\n",
       "      street_4     176      F\n",
       "      street_4     155      M\n",
       "      street_5     188      M\n",
       "      street_5     162      F\n",
       "      street_5     193      M\n",
       "      street_6     160      M\n",
       "      street_6     167      F\n",
       "      street_7     194      F\n",
       "      street_7     183      F"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>Height</th>\n      <th>Gender</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"10\" valign=\"top\">C_2</th>\n      <th>street_1</th>\n      <td>175</td>\n      <td>M</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>176</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>155</td>\n      <td>M</td>\n    </tr>\n    <tr>\n      <th>street_5</th>\n      <td>188</td>\n      <td>M</td>\n    </tr>\n    <tr>\n      <th>street_5</th>\n      <td>162</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <th>street_5</th>\n      <td>193</td>\n      <td>M</td>\n    </tr>\n    <tr>\n      <th>street_6</th>\n      <td>160</td>\n      <td>M</td>\n    </tr>\n    <tr>\n      <th>street_6</th>\n      <td>167</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>194</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>183</td>\n      <td>F</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 228
    }
   ],
   "source": [
    "df_using_mul.sort_index().loc[[('C_2','street_5')],['Height','Gender']]  #过滤行列要用括号.\n",
    "df_using_mul.sort_index().loc[[('C_2')],['Height','Gender']]  #过滤行列要用括号.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               School Gender  Height  Weight  Math Physics\n",
       "Class Address                                             \n",
       "C_2   street_6    S_1      M     160      53  58.8      A+\n",
       "      street_6    S_1      F     167      63  68.4      B-\n",
       "      street_7    S_2      F     194      77  68.5      B+\n",
       "      street_7    S_2      F     183      76  85.4       B\n",
       "C_3   street_1    S_1      F     175      57  87.7      A-\n",
       "      street_2    S_1      M     195      70  85.2       A\n",
       "      street_4    S_1      M     161      68  31.5      B+\n",
       "      street_4    S_2      F     157      78  72.3      B+\n",
       "      street_4    S_2      M     187      73  48.9       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"4\" valign=\"top\">C_2</th>\n      <th>street_6</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>160</td>\n      <td>53</td>\n      <td>58.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>street_6</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>167</td>\n      <td>63</td>\n      <td>68.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>194</td>\n      <td>77</td>\n      <td>68.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>183</td>\n      <td>76</td>\n      <td>85.4</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">C_3</th>\n      <th>street_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>175</td>\n      <td>57</td>\n      <td>87.7</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>195</td>\n      <td>70</td>\n      <td>85.2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>157</td>\n      <td>78</td>\n      <td>72.3</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_2</td>\n      <td>M</td>\n      <td>187</td>\n      <td>73</td>\n      <td>48.9</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 61
    }
   ],
   "source": [
    "#df_using_mul.loc[('C_2','street_5'):] #报错\n",
    "#当不排序时，不能使用多层切片\n",
    "df_using_mul.sort_index().loc[('C_2','street_6'):('C_3','street_4')]\n",
    "#注意此处由于使用了loc，因此仍然包含右端点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               School Gender  Height  Weight  Math Physics\n",
       "Class Address                                             \n",
       "C_2   street_7    S_2      F     194      77  68.5      B+\n",
       "      street_7    S_2      F     183      76  85.4       B\n",
       "C_3   street_1    S_1      F     175      57  87.7      A-\n",
       "      street_2    S_1      M     195      70  85.2       A\n",
       "      street_4    S_1      M     161      68  31.5      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">C_2</th>\n      <th>street_7</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>194</td>\n      <td>77</td>\n      <td>68.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>183</td>\n      <td>76</td>\n      <td>85.4</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">C_3</th>\n      <th>street_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>175</td>\n      <td>57</td>\n      <td>87.7</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>195</td>\n      <td>70</td>\n      <td>85.2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 62
    }
   ],
   "source": [
    "df_using_mul.sort_index().loc[('C_2','street_7'):'C_3'].head()\n",
    "#非元组也是合法的，表示选中该层所有元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）第一类特殊情况：由元组构成列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                Math Physics\n",
       "Class Address               \n",
       "C_2   street_7  68.5      B+\n",
       "      street_7  85.4       B\n",
       "C_3   street_2  85.2       A"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">C_2</th>\n      <th>street_7</th>\n      <td>68.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>85.4</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>C_3</th>\n      <th>street_2</th>\n      <td>85.2</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 225
    }
   ],
   "source": [
    "df_using_mul.sort_index().loc[[('C_2','street_7'),('C_3','street_2')]]\n",
    "df_using_mul.sort_index().loc[[('C_2','street_7'),('C_3','street_2')],['Math'\t,'Physics']] # 指定行和列\n",
    "#表示选出某几个元素，精确到最内层索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c）第二类特殊情况：由列表构成元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               School Gender  Height  Weight  Math Physics\n",
       "Class Address                                             \n",
       "C_2   street_4    S_1      F     176      94  63.5      B-\n",
       "      street_4    S_2      M     155      91  73.8      A+\n",
       "      street_7    S_2      F     194      77  68.5      B+\n",
       "      street_7    S_2      F     183      76  85.4       B\n",
       "C_3   street_4    S_1      M     161      68  31.5      B+\n",
       "      street_4    S_2      F     157      78  72.3      B+\n",
       "      street_4    S_2      M     187      73  48.9       B\n",
       "      street_7    S_1      M     188      82  49.7       B\n",
       "      street_7    S_2      F     190      99  65.9       C"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"4\" valign=\"top\">C_2</th>\n      <th>street_4</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>176</td>\n      <td>94</td>\n      <td>63.5</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_2</td>\n      <td>M</td>\n      <td>155</td>\n      <td>91</td>\n      <td>73.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>194</td>\n      <td>77</td>\n      <td>68.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>183</td>\n      <td>76</td>\n      <td>85.4</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">C_3</th>\n      <th>street_4</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>157</td>\n      <td>78</td>\n      <td>72.3</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_2</td>\n      <td>M</td>\n      <td>187</td>\n      <td>73</td>\n      <td>48.9</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>188</td>\n      <td>82</td>\n      <td>49.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>street_7</th>\n      <td>S_2</td>\n      <td>F</td>\n      <td>190</td>\n      <td>99</td>\n      <td>65.9</td>\n      <td>C</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 229
    }
   ],
   "source": [
    "df_using_mul.sort_index().loc[(['C_2','C_3'],['street_4','street_7']),:]\n",
    "#选出第一层在‘C_2’和'C_3'中且第二层在'street_4'和'street_7'中的行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 多层索引中的slice对象(切片对象)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 336,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Big                 D                             E                      \\\n",
       "Small               d         e         f         d         e         f   \n",
       "Upper Lower                                                               \n",
       "A     a      0.193412  0.699997  0.944616  0.060571  0.704936  0.805637   \n",
       "      b      0.097785  0.661147  0.508021  0.238928  0.437114  0.420944   \n",
       "      c      0.467085  0.919076  0.013426  0.391368  0.729596  0.000549   \n",
       "B     a      0.505154  0.934325  0.254364  0.597521  0.283975  0.809170   \n",
       "      b      0.588030  0.154554  0.022501  0.503805  0.883453  0.753349   \n",
       "      c      0.860484  0.183253  0.345607  0.444957  0.847323  0.655300   \n",
       "C     a      0.180993  0.729253  0.360716  0.479716  0.914596  0.103650   \n",
       "      b      0.860770  0.525331  0.703266  0.410602  0.973421  0.092868   \n",
       "      c      0.109495  0.993796  0.100804  0.456239  0.544351  0.437203   \n",
       "\n",
       "Big                 F                      \n",
       "Small               d         e         f  \n",
       "Upper Lower                                \n",
       "A     a      0.886872  0.942944  0.904215  \n",
       "      b      0.648056  0.734564  0.770847  \n",
       "      c      0.421235  0.660785  0.898848  \n",
       "B     a      0.305009  0.851463  0.122642  \n",
       "      b      0.485146  0.907196  0.374918  \n",
       "      c      0.839086  0.457206  0.262259  \n",
       "C     a      0.537803  0.332830  0.096457  \n",
       "      b      0.911406  0.714963  0.112832  \n",
       "      c      0.151247  0.896706  0.526724  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>Big</th>\n      <th colspan=\"3\" halign=\"left\">D</th>\n      <th colspan=\"3\" halign=\"left\">E</th>\n      <th colspan=\"3\" halign=\"left\">F</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>Small</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th>Lower</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">A</th>\n      <th>a</th>\n      <td>0.193412</td>\n      <td>0.699997</td>\n      <td>0.944616</td>\n      <td>0.060571</td>\n      <td>0.704936</td>\n      <td>0.805637</td>\n      <td>0.886872</td>\n      <td>0.942944</td>\n      <td>0.904215</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.097785</td>\n      <td>0.661147</td>\n      <td>0.508021</td>\n      <td>0.238928</td>\n      <td>0.437114</td>\n      <td>0.420944</td>\n      <td>0.648056</td>\n      <td>0.734564</td>\n      <td>0.770847</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.467085</td>\n      <td>0.919076</td>\n      <td>0.013426</td>\n      <td>0.391368</td>\n      <td>0.729596</td>\n      <td>0.000549</td>\n      <td>0.421235</td>\n      <td>0.660785</td>\n      <td>0.898848</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">B</th>\n      <th>a</th>\n      <td>0.505154</td>\n      <td>0.934325</td>\n      <td>0.254364</td>\n      <td>0.597521</td>\n      <td>0.283975</td>\n      <td>0.809170</td>\n      <td>0.305009</td>\n      <td>0.851463</td>\n      <td>0.122642</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.588030</td>\n      <td>0.154554</td>\n      <td>0.022501</td>\n      <td>0.503805</td>\n      <td>0.883453</td>\n      <td>0.753349</td>\n      <td>0.485146</td>\n      <td>0.907196</td>\n      <td>0.374918</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.860484</td>\n      <td>0.183253</td>\n      <td>0.345607</td>\n      <td>0.444957</td>\n      <td>0.847323</td>\n      <td>0.655300</td>\n      <td>0.839086</td>\n      <td>0.457206</td>\n      <td>0.262259</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">C</th>\n      <th>a</th>\n      <td>0.180993</td>\n      <td>0.729253</td>\n      <td>0.360716</td>\n      <td>0.479716</td>\n      <td>0.914596</td>\n      <td>0.103650</td>\n      <td>0.537803</td>\n      <td>0.332830</td>\n      <td>0.096457</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.860770</td>\n      <td>0.525331</td>\n      <td>0.703266</td>\n      <td>0.410602</td>\n      <td>0.973421</td>\n      <td>0.092868</td>\n      <td>0.911406</td>\n      <td>0.714963</td>\n      <td>0.112832</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.109495</td>\n      <td>0.993796</td>\n      <td>0.100804</td>\n      <td>0.456239</td>\n      <td>0.544351</td>\n      <td>0.437203</td>\n      <td>0.151247</td>\n      <td>0.896706</td>\n      <td>0.526724</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 336
    }
   ],
   "source": [
    "L1,L2 = ['A','B','C'],['a','b','c']\n",
    "mul_index1 = pd.MultiIndex.from_product([L1,L2],names=('Upper', 'Lower'))\n",
    "L3,L4 = ['D','E','F'],['d','e','f']\n",
    "mul_index2 = pd.MultiIndex.from_product([L3,L4],names=('Big', 'Small'))\n",
    "df_s = pd.DataFrame(np.random.rand(9,9),index=mul_index1,columns=mul_index2)\n",
    "df_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "metadata": {},
   "outputs": [],
   "source": [
    "idx=pd.IndexSlice  #创建索引切片对象."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 索引Slice的使用非常灵活："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Big                 D         E                   F                    \n",
       "Small               e         e         f         d         e         f\n",
       "Upper Lower                                                            \n",
       "B     a      0.934325  0.283975  0.809170  0.305009  0.851463  0.122642\n",
       "      b      0.154554  0.883453  0.753349  0.485146  0.907196  0.374918\n",
       "      c      0.183253  0.847323  0.655300  0.839086  0.457206  0.262259\n",
       "C     b      0.525331  0.973421  0.092868  0.911406  0.714963  0.112832"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>Big</th>\n      <th>D</th>\n      <th colspan=\"2\" halign=\"left\">E</th>\n      <th colspan=\"3\" halign=\"left\">F</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>Small</th>\n      <th>e</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th>Lower</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">B</th>\n      <th>a</th>\n      <td>0.934325</td>\n      <td>0.283975</td>\n      <td>0.809170</td>\n      <td>0.305009</td>\n      <td>0.851463</td>\n      <td>0.122642</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.154554</td>\n      <td>0.883453</td>\n      <td>0.753349</td>\n      <td>0.485146</td>\n      <td>0.907196</td>\n      <td>0.374918</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.183253</td>\n      <td>0.847323</td>\n      <td>0.655300</td>\n      <td>0.839086</td>\n      <td>0.457206</td>\n      <td>0.262259</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <th>b</th>\n      <td>0.525331</td>\n      <td>0.973421</td>\n      <td>0.092868</td>\n      <td>0.911406</td>\n      <td>0.714963</td>\n      <td>0.112832</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 337
    }
   ],
   "source": [
    "df_s.loc[idx['B':,df_s['D']['d']>0.3],idx[df_s.sum()>4]]\n",
    "#df_s.sum()默认为对列求和，因此返回一个长度为9的数值列表\n",
    "#idx['B':,df_s['D']['d']>0.3] :: 选择['B':]行 与 df_s['D']['d']列>0.3的行的行索引\n",
    "#idx[df_s.sum()>4] :: 选择df_s.sum()>4 的列索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 索引层的交换\n",
    "#### （a）swaplevel方法（两层交换）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               School Gender  Height  Weight  Math Physics\n",
       "Class Address                                             \n",
       "C_1   street_1    S_1      M     173      63  34.0      A+\n",
       "      street_2    S_1      F     192      73  32.5      B+\n",
       "      street_2    S_1      M     186      82  87.2      B+\n",
       "      street_2    S_1      F     167      81  80.4      B-\n",
       "      street_4    S_1      F     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">C_1</th>\n      <th>street_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 68
    }
   ],
   "source": [
    "df_using_mul.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "               School Gender  Height  Weight  Math Physics\n",
       "Address  Class                                            \n",
       "street_1 C_1      S_1      M     173      63  34.0      A+\n",
       "         C_2      S_2      M     175      74  47.2      B-\n",
       "         C_3      S_1      F     175      57  87.7      A-\n",
       "street_2 C_1      S_1      F     192      73  32.5      B+\n",
       "         C_1      S_1      M     186      82  87.2      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Address</th>\n      <th>Class</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">street_1</th>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>C_2</th>\n      <td>S_2</td>\n      <td>M</td>\n      <td>175</td>\n      <td>74</td>\n      <td>47.2</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>C_3</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>175</td>\n      <td>57</td>\n      <td>87.7</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">street_2</th>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 69
    }
   ],
   "source": [
    "df_using_mul.swaplevel(i=1,j=0,axis=0).sort_index().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）reorder_levels方法（多层交换）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 340,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                      Gender  Height  Weight  Math Physics\n",
       "School Class Address                                      \n",
       "S_1    C_1   street_1      M     173      63  34.0      A+\n",
       "             street_2      F     192      73  32.5      B+\n",
       "             street_2      M     186      82  87.2      B+\n",
       "             street_2      F     167      81  80.4      B-\n",
       "             street_4      F     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th></th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>School</th>\n      <th>Class</th>\n      <th>Address</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"5\" valign=\"top\">S_1</th>\n      <th rowspan=\"5\" valign=\"top\">C_1</th>\n      <th>street_1</th>\n      <td>M</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>F</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>M</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>street_2</th>\n      <td>F</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <td>F</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 340
    }
   ],
   "source": [
    "df_muls = df.set_index(['School','Class','Address'])\n",
    "df_muls.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                      Gender  Height  Weight  Math Physics\n",
       "Address  School Class                                     \n",
       "street_1 S_1    C_1        M     173      63  34.0      A+\n",
       "                C_3        F     175      57  87.7      A-\n",
       "         S_2    C_2        M     175      74  47.2      B-\n",
       "street_2 S_1    C_1        F     192      73  32.5      B+\n",
       "                C_1        M     186      82  87.2      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th></th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Address</th>\n      <th>School</th>\n      <th>Class</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">street_1</th>\n      <th rowspan=\"2\" valign=\"top\">S_1</th>\n      <th>C_1</th>\n      <td>M</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>C_3</th>\n      <td>F</td>\n      <td>175</td>\n      <td>57</td>\n      <td>87.7</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>S_2</th>\n      <th>C_2</th>\n      <td>M</td>\n      <td>175</td>\n      <td>74</td>\n      <td>47.2</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">street_2</th>\n      <th rowspan=\"2\" valign=\"top\">S_1</th>\n      <th>C_1</th>\n      <td>F</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>C_1</th>\n      <td>M</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 71
    }
   ],
   "source": [
    "df_muls.reorder_levels([2,0,1],axis=0).sort_index().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                      Gender  Height  Weight  Math Physics\n",
       "Address  School Class                                     \n",
       "street_1 S_1    C_1        M     173      63  34.0      A+\n",
       "                C_3        F     175      57  87.7      A-\n",
       "         S_2    C_2        M     175      74  47.2      B-\n",
       "street_2 S_1    C_1        F     192      73  32.5      B+\n",
       "                C_1        M     186      82  87.2      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th></th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Address</th>\n      <th>School</th>\n      <th>Class</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">street_1</th>\n      <th rowspan=\"2\" valign=\"top\">S_1</th>\n      <th>C_1</th>\n      <td>M</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>C_3</th>\n      <td>F</td>\n      <td>175</td>\n      <td>57</td>\n      <td>87.7</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>S_2</th>\n      <th>C_2</th>\n      <td>M</td>\n      <td>175</td>\n      <td>74</td>\n      <td>47.2</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">street_2</th>\n      <th rowspan=\"2\" valign=\"top\">S_1</th>\n      <th>C_1</th>\n      <td>F</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>C_1</th>\n      <td>M</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 72
    }
   ],
   "source": [
    "#如果索引有name，可以直接使用name\n",
    "df_muls.reorder_levels(['Address','School','Class'],axis=0).sort_index().head() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、索引设定\n",
    "### 1. index_col参数\n",
    "#### index_col是read_csv中的一个参数，而不是某一个方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "                Class    ID Gender  Height  Weight  Math Physics\n",
       "Address  School                                                 \n",
       "street_1 S_1      C_1  1101      M     173      63  34.0      A+\n",
       "street_2 S_1      C_1  1102      F     192      73  32.5      B+\n",
       "         S_1      C_1  1103      M     186      82  87.2      B+\n",
       "         S_1      C_1  1104      F     167      81  80.4      B-\n",
       "street_4 S_1      C_1  1105      F     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>Class</th>\n      <th>ID</th>\n      <th>Gender</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Address</th>\n      <th>School</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>street_1</th>\n      <th>S_1</th>\n      <td>C_1</td>\n      <td>1101</td>\n      <td>M</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">street_2</th>\n      <th>S_1</th>\n      <td>C_1</td>\n      <td>1102</td>\n      <td>F</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>S_1</th>\n      <td>C_1</td>\n      <td>1103</td>\n      <td>M</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>S_1</th>\n      <td>C_1</td>\n      <td>1104</td>\n      <td>F</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>street_4</th>\n      <th>S_1</th>\n      <td>C_1</td>\n      <td>1105</td>\n      <td>F</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 73
    }
   ],
   "source": [
    "pd.read_csv('data/table.csv',index_col=['Address','School']).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. reindex和reindex_like\n",
    "#### reindex是指重新索引，它的重要特性在于索引对齐，很多时候用于重新排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 74
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1   173.0    63.0  34.0      A+\n",
       "1203    S_1   C_2      M  street_6   160.0    53.0  58.8      A+\n",
       "1206    NaN   NaN    NaN       NaN     NaN     NaN   NaN     NaN\n",
       "2402    S_2   C_4      M  street_7   166.0    82.0  48.7       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173.0</td>\n      <td>63.0</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_6</td>\n      <td>160.0</td>\n      <td>53.0</td>\n      <td>58.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1206</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2402</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>166.0</td>\n      <td>82.0</td>\n      <td>48.7</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 75
    }
   ],
   "source": [
    "df.reindex(index=[1101,1203,1206,2402])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      Height Gender  Average\n",
       "ID                          \n",
       "1101     173      M      NaN\n",
       "1102     192      F      NaN\n",
       "1103     186      M      NaN\n",
       "1104     167      F      NaN\n",
       "1105     159      F      NaN"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Height</th>\n      <th>Gender</th>\n      <th>Average</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>173</td>\n      <td>M</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>192</td>\n      <td>F</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>186</td>\n      <td>M</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>167</td>\n      <td>F</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>159</td>\n      <td>F</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 76
    }
   ],
   "source": [
    "df.reindex(columns=['Height','Gender','Average']).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 可以选择缺失值的填充方法：fill_value和method（bfill/ffill/nearest），其中method参数必须索引单调"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1203    S_1   C_2      M  street_6     160      53  58.8      A+\n",
       "1206    S_1   C_3      M  street_4     161      68  31.5      B+\n",
       "2402    S_2   C_4      M  street_7     166      82  48.7       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_6</td>\n      <td>160</td>\n      <td>53</td>\n      <td>58.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1206</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2402</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>166</td>\n      <td>82</td>\n      <td>48.7</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 77
    }
   ],
   "source": [
    "df.reindex(index=[1101,1203,1206,2402],method='bfill')\n",
    "#bfill表示用所在索引1206的后一个有效行填充，ffill为前一个有效行，nearest是指最近的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1203    S_1   C_2      M  street_6     160      53  58.8      A+\n",
       "1206    S_1   C_2      F  street_6     167      63  68.4      B-\n",
       "2402    S_2   C_4      M  street_7     166      82  48.7       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_6</td>\n      <td>160</td>\n      <td>53</td>\n      <td>58.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1206</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>167</td>\n      <td>63</td>\n      <td>68.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>2402</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>166</td>\n      <td>82</td>\n      <td>48.7</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 78
    }
   ],
   "source": [
    "df.reindex(index=[1101,1203,1206,2402],method='nearest')\n",
    "#数值上1205比1301更接近1206，因此用前者填充"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### reindex_like的作用为生成一个横纵索引完全与参数列表一致的DataFrame，数据使用被调用的表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      Weight  Height\n",
       "ID                  \n",
       "1101     0.0     0.0\n",
       "1102     0.0     0.0\n",
       "1103     0.0     0.0\n",
       "1104     0.0     0.0\n",
       "1105     NaN     NaN\n",
       "1201     NaN     NaN\n",
       "1202     NaN     NaN\n",
       "1203     NaN     NaN\n",
       "1204     NaN     NaN\n",
       "1205     NaN     NaN\n",
       "1301     NaN     NaN\n",
       "1302     NaN     NaN"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Weight</th>\n      <th>Height</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1204</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1205</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1302</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 59
    }
   ],
   "source": [
    "df_temp = pd.DataFrame({'Weight':np.zeros(5),\n",
    "                        'Height':np.zeros(5),\n",
    "                        'ID':[1101,1104,1103,1106,1102]}).set_index('ID')\n",
    "df_temp.reindex_like(df[0:12][['Weight','Height']])  # 按df的索引生成索引号 并且 用df_temp填充数据 并且'索引对齐'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 如果df_temp单调还可以使用method参数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      Weight  Height\n",
       "ID                  \n",
       "1101     0.0     0.0\n",
       "1102     4.0     4.0\n",
       "1103     2.0     2.0\n",
       "1104     1.0     1.0\n",
       "1105     3.0     3.0\n",
       "1201     NaN     NaN\n",
       "1202     NaN     NaN\n",
       "1203     NaN     NaN\n",
       "1204     NaN     NaN\n",
       "1205     NaN     NaN\n",
       "1301     NaN     NaN\n",
       "1302     NaN     NaN"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Weight</th>\n      <th>Height</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>4.0</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>2.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>1.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>3.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1204</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1205</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1302</th>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 60
    }
   ],
   "source": [
    "df_temp = pd.DataFrame({'Weight':range(5),\n",
    "                        'Height':range(5),\n",
    "                        'ID':[1101,1104,1103,1106,1102]}).set_index('ID').sort_index()\n",
    "df_temp.reindex_like(df[0:12][['Weight','Height']],method='bfill')\n",
    "#可以自行检验这里的1105的值是否是由bfill规则填充"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. set_index和reset_index\n",
    "#### 先介绍set_index：从字面意思看，就是将某些列作为索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 使用表内列作为索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 61
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      School Gender   Address  Height  Weight  Math Physics\n",
       "Class                                                      \n",
       "C_1      S_1      M  street_1     173      63  34.0      A+\n",
       "C_1      S_1      F  street_2     192      73  32.5      B+\n",
       "C_1      S_1      M  street_2     186      82  87.2      B+\n",
       "C_1      S_1      F  street_2     167      81  80.4      B-\n",
       "C_1      S_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>Class</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 82
    }
   ],
   "source": [
    "df.set_index('Class').head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 利用append参数可以将当前索引维持不变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "           School Gender   Address  Height  Weight  Math Physics\n",
       "ID   Class                                                      \n",
       "1101 C_1      S_1      M  street_1     173      63  34.0      A+\n",
       "1102 C_1      S_1      F  street_2     192      73  32.5      B+\n",
       "1103 C_1      S_1      M  street_2     186      82  87.2      B+\n",
       "1104 C_1      S_1      F  street_2     167      81  80.4      B-\n",
       "1105 C_1      S_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th>Class</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <th>C_1</th>\n      <td>S_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 83
    }
   ],
   "source": [
    "df.set_index('Class',append=True).head()  # 在原索引基础上追加索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 当使用与表长相同的列作为索引（需要先转化为Series，否则报错）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "  School Class Gender   Address  Height  Weight  Math Physics\n",
       "0    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "2    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "3    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "4    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 84
    }
   ],
   "source": [
    "df.set_index(pd.Series(range(df.shape[0]))).head() # 引用Series作为索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 可以直接添加多级索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "      School Class Gender   Address  Height  Weight  Math Physics\n",
       "0 1.0    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1 1.0    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "2 1.0    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "3 1.0    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "4 1.0    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <th>1.0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <th>1.0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <th>1.0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <th>1.0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <th>1.0</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 85
    }
   ],
   "source": [
    "df.set_index([pd.Series(range(df.shape[0])),pd.Series(np.ones(df.shape[0]))]).head() # 引用Series作为多重索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 下面介绍reset_index方法，它的主要功能是将索引重置\n",
    "#### 默认状态直接恢复到自然数索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     ID School Class Gender   Address  Height  Weight  Math Physics\n",
       "0  1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1  1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "2  1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "3  1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "4  1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>ID</th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1101</td>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1102</td>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1103</td>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1104</td>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1105</td>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 86
    }
   ],
   "source": [
    "df.reset_index().head()"
   ]
  },
  {
   "source": [
    "#### 用level参数指定哪一层被reset，用col_level参数指定set到哪一层：\n",
    "注:用level参数指定哪一层index被reset，原后用col_level参数指定将重置index ,set到哪一层columns："
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Big                 D                             E                      \\\n",
       "Small               d         e         f         d         e         f   \n",
       "Upper Lower                                                               \n",
       "A     a      0.580254  0.652483  0.810430  0.677723  0.172970  0.149027   \n",
       "      b      0.639799  0.316541  0.425662  0.340557  0.155241  0.969011   \n",
       "      c      0.140157  0.020944  0.425067  0.736703  0.778201  0.230059   \n",
       "B     a      0.932446  0.240970  0.337224  0.194086  0.408749  0.697972   \n",
       "      b      0.280849  0.463129  0.320851  0.308956  0.729003  0.006592   \n",
       "\n",
       "Big                 F                      \n",
       "Small               d         e         f  \n",
       "Upper Lower                                \n",
       "A     a      0.985820  0.906975  0.478051  \n",
       "      b      0.538653  0.851291  0.948741  \n",
       "      c      0.019073  0.549400  0.158975  \n",
       "B     a      0.332927  0.916338  0.028605  \n",
       "      b      0.310519  0.658612  0.740653  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>Big</th>\n      <th colspan=\"3\" halign=\"left\">D</th>\n      <th colspan=\"3\" halign=\"left\">E</th>\n      <th colspan=\"3\" halign=\"left\">F</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>Small</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th>Lower</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">A</th>\n      <th>a</th>\n      <td>0.580254</td>\n      <td>0.652483</td>\n      <td>0.810430</td>\n      <td>0.677723</td>\n      <td>0.172970</td>\n      <td>0.149027</td>\n      <td>0.985820</td>\n      <td>0.906975</td>\n      <td>0.478051</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.639799</td>\n      <td>0.316541</td>\n      <td>0.425662</td>\n      <td>0.340557</td>\n      <td>0.155241</td>\n      <td>0.969011</td>\n      <td>0.538653</td>\n      <td>0.851291</td>\n      <td>0.948741</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.140157</td>\n      <td>0.020944</td>\n      <td>0.425067</td>\n      <td>0.736703</td>\n      <td>0.778201</td>\n      <td>0.230059</td>\n      <td>0.019073</td>\n      <td>0.549400</td>\n      <td>0.158975</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">B</th>\n      <th>a</th>\n      <td>0.932446</td>\n      <td>0.240970</td>\n      <td>0.337224</td>\n      <td>0.194086</td>\n      <td>0.408749</td>\n      <td>0.697972</td>\n      <td>0.332927</td>\n      <td>0.916338</td>\n      <td>0.028605</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.280849</td>\n      <td>0.463129</td>\n      <td>0.320851</td>\n      <td>0.308956</td>\n      <td>0.729003</td>\n      <td>0.006592</td>\n      <td>0.310519</td>\n      <td>0.658612</td>\n      <td>0.740653</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 87
    }
   ],
   "source": [
    "L1,L2 = ['A','B','C'],['a','b','c']\n",
    "mul_index1 = pd.MultiIndex.from_product([L1,L2],names=('Upper', 'Lower'))\n",
    "L3,L4 = ['D','E','F'],['d','e','f']\n",
    "mul_index2 = pd.MultiIndex.from_product([L3,L4],names=('Big', 'Small'))\n",
    "df_temp = pd.DataFrame(np.random.rand(9,9),index=mul_index1,columns=mul_index2)\n",
    "df_temp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Big                 D                             E                      \\\n",
       "Small Lower         d         e         f         d         e         f   \n",
       "Upper                                                                     \n",
       "A         a  0.580254  0.652483  0.810430  0.677723  0.172970  0.149027   \n",
       "A         b  0.639799  0.316541  0.425662  0.340557  0.155241  0.969011   \n",
       "A         c  0.140157  0.020944  0.425067  0.736703  0.778201  0.230059   \n",
       "B         a  0.932446  0.240970  0.337224  0.194086  0.408749  0.697972   \n",
       "B         b  0.280849  0.463129  0.320851  0.308956  0.729003  0.006592   \n",
       "\n",
       "Big           F                      \n",
       "Small         d         e         f  \n",
       "Upper                                \n",
       "A      0.985820  0.906975  0.478051  \n",
       "A      0.538653  0.851291  0.948741  \n",
       "A      0.019073  0.549400  0.158975  \n",
       "B      0.332927  0.916338  0.028605  \n",
       "B      0.310519  0.658612  0.740653  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th>Big</th>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">D</th>\n      <th colspan=\"3\" halign=\"left\">E</th>\n      <th colspan=\"3\" halign=\"left\">F</th>\n    </tr>\n    <tr>\n      <th>Small</th>\n      <th>Lower</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>a</td>\n      <td>0.580254</td>\n      <td>0.652483</td>\n      <td>0.810430</td>\n      <td>0.677723</td>\n      <td>0.172970</td>\n      <td>0.149027</td>\n      <td>0.985820</td>\n      <td>0.906975</td>\n      <td>0.478051</td>\n    </tr>\n    <tr>\n      <th>A</th>\n      <td>b</td>\n      <td>0.639799</td>\n      <td>0.316541</td>\n      <td>0.425662</td>\n      <td>0.340557</td>\n      <td>0.155241</td>\n      <td>0.969011</td>\n      <td>0.538653</td>\n      <td>0.851291</td>\n      <td>0.948741</td>\n    </tr>\n    <tr>\n      <th>A</th>\n      <td>c</td>\n      <td>0.140157</td>\n      <td>0.020944</td>\n      <td>0.425067</td>\n      <td>0.736703</td>\n      <td>0.778201</td>\n      <td>0.230059</td>\n      <td>0.019073</td>\n      <td>0.549400</td>\n      <td>0.158975</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>a</td>\n      <td>0.932446</td>\n      <td>0.240970</td>\n      <td>0.337224</td>\n      <td>0.194086</td>\n      <td>0.408749</td>\n      <td>0.697972</td>\n      <td>0.332927</td>\n      <td>0.916338</td>\n      <td>0.028605</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>b</td>\n      <td>0.280849</td>\n      <td>0.463129</td>\n      <td>0.320851</td>\n      <td>0.308956</td>\n      <td>0.729003</td>\n      <td>0.006592</td>\n      <td>0.310519</td>\n      <td>0.658612</td>\n      <td>0.740653</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 88
    }
   ],
   "source": [
    "df_temp1 = df_temp.reset_index(level=1,col_level=1)\n",
    "df_temp1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['D', 'E', 'F', ''], ['d', 'e', 'f', 'Lower']],\n",
       "           codes=[[3, 0, 0, 0, 1, 1, 1, 2, 2, 2], [3, 0, 1, 2, 0, 1, 2, 0, 1, 2]],\n",
       "           names=['Big', 'Small'])"
      ]
     },
     "metadata": {},
     "execution_count": 89
    }
   ],
   "source": [
    "df_temp1.columns\n",
    "#看到的确插入了level2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Index(['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'], dtype='object', name='Upper')"
      ]
     },
     "metadata": {},
     "execution_count": 90
    }
   ],
   "source": [
    "df_temp1.index\n",
    "#最内层索引被移出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. rename_axis和rename\n",
    "#### rename_axis是针对多级索引的方法，作用是修改某一层的索引名，而不是索引标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "BigBig                   D                             E                      \\\n",
       "Small                    d         e         f         d         e         f   \n",
       "Upper LowerLower                                                               \n",
       "A     a           0.580254  0.652483  0.810430  0.677723  0.172970  0.149027   \n",
       "      b           0.639799  0.316541  0.425662  0.340557  0.155241  0.969011   \n",
       "      c           0.140157  0.020944  0.425067  0.736703  0.778201  0.230059   \n",
       "B     a           0.932446  0.240970  0.337224  0.194086  0.408749  0.697972   \n",
       "      b           0.280849  0.463129  0.320851  0.308956  0.729003  0.006592   \n",
       "      c           0.097764  0.288550  0.254437  0.009656  0.338892  0.797935   \n",
       "C     a           0.245393  0.085986  0.646461  0.746386  0.418474  0.810499   \n",
       "      b           0.845498  0.704880  0.889704  0.635467  0.995029  0.045432   \n",
       "      c           0.484682  0.261331  0.345319  0.300768  0.652754  0.988572   \n",
       "\n",
       "BigBig                   F                      \n",
       "Small                    d         e         f  \n",
       "Upper LowerLower                                \n",
       "A     a           0.985820  0.906975  0.478051  \n",
       "      b           0.538653  0.851291  0.948741  \n",
       "      c           0.019073  0.549400  0.158975  \n",
       "B     a           0.332927  0.916338  0.028605  \n",
       "      b           0.310519  0.658612  0.740653  \n",
       "      c           0.132962  0.857680  0.877342  \n",
       "C     a           0.263449  0.509328  0.144601  \n",
       "      b           0.040453  0.015194  0.685606  \n",
       "      c           0.526525  0.340548  0.091199  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>BigBig</th>\n      <th colspan=\"3\" halign=\"left\">D</th>\n      <th colspan=\"3\" halign=\"left\">E</th>\n      <th colspan=\"3\" halign=\"left\">F</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>Small</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th>LowerLower</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">A</th>\n      <th>a</th>\n      <td>0.580254</td>\n      <td>0.652483</td>\n      <td>0.810430</td>\n      <td>0.677723</td>\n      <td>0.172970</td>\n      <td>0.149027</td>\n      <td>0.985820</td>\n      <td>0.906975</td>\n      <td>0.478051</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.639799</td>\n      <td>0.316541</td>\n      <td>0.425662</td>\n      <td>0.340557</td>\n      <td>0.155241</td>\n      <td>0.969011</td>\n      <td>0.538653</td>\n      <td>0.851291</td>\n      <td>0.948741</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.140157</td>\n      <td>0.020944</td>\n      <td>0.425067</td>\n      <td>0.736703</td>\n      <td>0.778201</td>\n      <td>0.230059</td>\n      <td>0.019073</td>\n      <td>0.549400</td>\n      <td>0.158975</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">B</th>\n      <th>a</th>\n      <td>0.932446</td>\n      <td>0.240970</td>\n      <td>0.337224</td>\n      <td>0.194086</td>\n      <td>0.408749</td>\n      <td>0.697972</td>\n      <td>0.332927</td>\n      <td>0.916338</td>\n      <td>0.028605</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.280849</td>\n      <td>0.463129</td>\n      <td>0.320851</td>\n      <td>0.308956</td>\n      <td>0.729003</td>\n      <td>0.006592</td>\n      <td>0.310519</td>\n      <td>0.658612</td>\n      <td>0.740653</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.097764</td>\n      <td>0.288550</td>\n      <td>0.254437</td>\n      <td>0.009656</td>\n      <td>0.338892</td>\n      <td>0.797935</td>\n      <td>0.132962</td>\n      <td>0.857680</td>\n      <td>0.877342</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">C</th>\n      <th>a</th>\n      <td>0.245393</td>\n      <td>0.085986</td>\n      <td>0.646461</td>\n      <td>0.746386</td>\n      <td>0.418474</td>\n      <td>0.810499</td>\n      <td>0.263449</td>\n      <td>0.509328</td>\n      <td>0.144601</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.845498</td>\n      <td>0.704880</td>\n      <td>0.889704</td>\n      <td>0.635467</td>\n      <td>0.995029</td>\n      <td>0.045432</td>\n      <td>0.040453</td>\n      <td>0.015194</td>\n      <td>0.685606</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.484682</td>\n      <td>0.261331</td>\n      <td>0.345319</td>\n      <td>0.300768</td>\n      <td>0.652754</td>\n      <td>0.988572</td>\n      <td>0.526525</td>\n      <td>0.340548</td>\n      <td>0.091199</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 91
    }
   ],
   "source": [
    "df_temp.rename_axis(index={'Lower':'LowerLower'},columns={'Big':'BigBig'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### rename方法用于修改列或者行索引标签，而不是索引名："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "Big                 D                             E                      \\\n",
       "Small               d changed_e         f         d changed_e         f   \n",
       "Upper Lower                                                               \n",
       "T     a      0.580254  0.652483  0.810430  0.677723  0.172970  0.149027   \n",
       "      b      0.639799  0.316541  0.425662  0.340557  0.155241  0.969011   \n",
       "      c      0.140157  0.020944  0.425067  0.736703  0.778201  0.230059   \n",
       "B     a      0.932446  0.240970  0.337224  0.194086  0.408749  0.697972   \n",
       "      b      0.280849  0.463129  0.320851  0.308956  0.729003  0.006592   \n",
       "\n",
       "Big                 F                      \n",
       "Small               d changed_e         f  \n",
       "Upper Lower                                \n",
       "T     a      0.985820  0.906975  0.478051  \n",
       "      b      0.538653  0.851291  0.948741  \n",
       "      c      0.019073  0.549400  0.158975  \n",
       "B     a      0.332927  0.916338  0.028605  \n",
       "      b      0.310519  0.658612  0.740653  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>Big</th>\n      <th colspan=\"3\" halign=\"left\">D</th>\n      <th colspan=\"3\" halign=\"left\">E</th>\n      <th colspan=\"3\" halign=\"left\">F</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>Small</th>\n      <th>d</th>\n      <th>changed_e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>changed_e</th>\n      <th>f</th>\n      <th>d</th>\n      <th>changed_e</th>\n      <th>f</th>\n    </tr>\n    <tr>\n      <th>Upper</th>\n      <th>Lower</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">T</th>\n      <th>a</th>\n      <td>0.580254</td>\n      <td>0.652483</td>\n      <td>0.810430</td>\n      <td>0.677723</td>\n      <td>0.172970</td>\n      <td>0.149027</td>\n      <td>0.985820</td>\n      <td>0.906975</td>\n      <td>0.478051</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.639799</td>\n      <td>0.316541</td>\n      <td>0.425662</td>\n      <td>0.340557</td>\n      <td>0.155241</td>\n      <td>0.969011</td>\n      <td>0.538653</td>\n      <td>0.851291</td>\n      <td>0.948741</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>0.140157</td>\n      <td>0.020944</td>\n      <td>0.425067</td>\n      <td>0.736703</td>\n      <td>0.778201</td>\n      <td>0.230059</td>\n      <td>0.019073</td>\n      <td>0.549400</td>\n      <td>0.158975</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">B</th>\n      <th>a</th>\n      <td>0.932446</td>\n      <td>0.240970</td>\n      <td>0.337224</td>\n      <td>0.194086</td>\n      <td>0.408749</td>\n      <td>0.697972</td>\n      <td>0.332927</td>\n      <td>0.916338</td>\n      <td>0.028605</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>0.280849</td>\n      <td>0.463129</td>\n      <td>0.320851</td>\n      <td>0.308956</td>\n      <td>0.729003</td>\n      <td>0.006592</td>\n      <td>0.310519</td>\n      <td>0.658612</td>\n      <td>0.740653</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 92
    }
   ],
   "source": [
    "df_temp.rename(index={'A':'T'},columns={'e':'changed_e'}).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四、常用索引型函数\n",
    "### 1. where函数\n",
    "#### 当对条件为False的单元进行填充："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 93
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1   173.0    63.0  34.0      A+\n",
       "1102    NaN   NaN    NaN       NaN     NaN     NaN   NaN     NaN\n",
       "1103    S_1   C_1      M  street_2   186.0    82.0  87.2      B+\n",
       "1104    NaN   NaN    NaN       NaN     NaN     NaN   NaN     NaN\n",
       "1105    NaN   NaN    NaN       NaN     NaN     NaN   NaN     NaN"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173.0</td>\n      <td>63.0</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186.0</td>\n      <td>82.0</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 94
    }
   ],
   "source": [
    "df.where(df['Gender']=='M').head()\n",
    "#不满足条件的行全部被设置为NaN"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 通过这种方法筛选结果和[]操作符的结果完全一致："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1   173.0    63.0  34.0      A+\n",
       "1103    S_1   C_1      M  street_2   186.0    82.0  87.2      B+\n",
       "1201    S_1   C_2      M  street_5   188.0    68.0  97.0      A-\n",
       "1203    S_1   C_2      M  street_6   160.0    53.0  58.8      A+\n",
       "1301    S_1   C_3      M  street_4   161.0    68.0  31.5      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173.0</td>\n      <td>63.0</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186.0</td>\n      <td>82.0</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_5</td>\n      <td>188.0</td>\n      <td>68.0</td>\n      <td>97.0</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>1203</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_6</td>\n      <td>160.0</td>\n      <td>53.0</td>\n      <td>58.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161.0</td>\n      <td>68.0</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 95
    }
   ],
   "source": [
    "df.where(df['Gender']=='M').dropna().head()"
   ]
  },
  {
   "source": [
    "#### 第一个参数为布尔条件，第二个参数为填充值：\n",
    "pandas where函数用法  https://zhuanlan.zhihu.com/p/107050664"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "        School     Class    Gender    Address      Height     Weight  \\\n",
       "ID                                                                     \n",
       "1101       S_1       C_1         M   street_1  173.000000  63.000000   \n",
       "1102  0.279661  0.827745  0.573646   0.412688    0.036816   0.179259   \n",
       "1103       S_1       C_1         M   street_2  186.000000  82.000000   \n",
       "1104  0.276022  0.521454  0.423225  0.0229911    0.119860   0.491388   \n",
       "1105  0.371232   0.72461  0.347034   0.113382    0.851998   0.667376   \n",
       "\n",
       "           Math   Physics  \n",
       "ID                         \n",
       "1101  34.000000        A+  \n",
       "1102   0.164816  0.789835  \n",
       "1103  87.200000        B+  \n",
       "1104   0.442320  0.808056  \n",
       "1105   0.220981  0.443459  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173.000000</td>\n      <td>63.000000</td>\n      <td>34.000000</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>0.279661</td>\n      <td>0.827745</td>\n      <td>0.573646</td>\n      <td>0.412688</td>\n      <td>0.036816</td>\n      <td>0.179259</td>\n      <td>0.164816</td>\n      <td>0.789835</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186.000000</td>\n      <td>82.000000</td>\n      <td>87.200000</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>0.276022</td>\n      <td>0.521454</td>\n      <td>0.423225</td>\n      <td>0.0229911</td>\n      <td>0.119860</td>\n      <td>0.491388</td>\n      <td>0.442320</td>\n      <td>0.808056</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>0.371232</td>\n      <td>0.72461</td>\n      <td>0.347034</td>\n      <td>0.113382</td>\n      <td>0.851998</td>\n      <td>0.667376</td>\n      <td>0.220981</td>\n      <td>0.443459</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 96
    }
   ],
   "source": [
    "df.where(df['Gender']=='M',np.random.rand(df.shape[0],df.shape[1])).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. mask函数\n",
    "#### mask函数与where功能上相反，其余完全一致，即对条件为True的单元进行填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1102    S_1   C_1      F  street_2   192.0    73.0  32.5      B+\n",
       "1104    S_1   C_1      F  street_2   167.0    81.0  80.4      B-\n",
       "1105    S_1   C_1      F  street_4   159.0    64.0  84.8      B+\n",
       "1202    S_1   C_2      F  street_4   176.0    94.0  63.5      B-\n",
       "1204    S_1   C_2      F  street_5   162.0    63.0  33.8       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192.0</td>\n      <td>73.0</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167.0</td>\n      <td>81.0</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159.0</td>\n      <td>64.0</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1202</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>176.0</td>\n      <td>94.0</td>\n      <td>63.5</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1204</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_5</td>\n      <td>162.0</td>\n      <td>63.0</td>\n      <td>33.8</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 97
    }
   ],
   "source": [
    "df.mask(df['Gender']=='M').dropna().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "        School     Class    Gender   Address      Height     Weight  \\\n",
       "ID                                                                    \n",
       "1101  0.273773   0.32025  0.208572  0.797227    0.311067   0.997344   \n",
       "1102       S_1       C_1         F  street_2  192.000000  73.000000   \n",
       "1103  0.764557  0.330236  0.215397  0.331227    0.821597   0.735847   \n",
       "1104       S_1       C_1         F  street_2  167.000000  81.000000   \n",
       "1105       S_1       C_1         F  street_4  159.000000  64.000000   \n",
       "\n",
       "           Math   Physics  \n",
       "ID                         \n",
       "1101   0.204049  0.366703  \n",
       "1102  32.500000        B+  \n",
       "1103   0.983437  0.927336  \n",
       "1104  80.400000        B-  \n",
       "1105  84.800000        B+  "
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>0.273773</td>\n      <td>0.32025</td>\n      <td>0.208572</td>\n      <td>0.797227</td>\n      <td>0.311067</td>\n      <td>0.997344</td>\n      <td>0.204049</td>\n      <td>0.366703</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192.000000</td>\n      <td>73.000000</td>\n      <td>32.500000</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>0.764557</td>\n      <td>0.330236</td>\n      <td>0.215397</td>\n      <td>0.331227</td>\n      <td>0.821597</td>\n      <td>0.735847</td>\n      <td>0.983437</td>\n      <td>0.927336</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167.000000</td>\n      <td>81.000000</td>\n      <td>80.400000</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159.000000</td>\n      <td>64.000000</td>\n      <td>84.800000</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 98
    }
   ],
   "source": [
    "df.mask(df['Gender']=='M',np.random.rand(df.shape[0],df.shape[1])).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. query函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1102</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>73</td>\n      <td>32.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1103</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_2</td>\n      <td>186</td>\n      <td>82</td>\n      <td>87.2</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>1105</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>159</td>\n      <td>64</td>\n      <td>84.8</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 99
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### query函数中的布尔表达式中，下面的符号都是合法的：行列索引名、字符串、and/not/or/&/|/~/not in/in/==/!=、四则运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1303    S_1   C_3      M  street_7     188      82  49.7       B\n",
       "2304    S_2   C_3      F  street_6     164      81  95.5      A-\n",
       "2402    S_2   C_4      M  street_7     166      82  48.7       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1303</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>188</td>\n      <td>82</td>\n      <td>49.7</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2304</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>164</td>\n      <td>81</td>\n      <td>95.5</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>2402</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>166</td>\n      <td>82</td>\n      <td>48.7</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 100
    }
   ],
   "source": [
    "df.query('(Address in [\"street_6\",\"street_7\"])&(Weight>(70+10))&(ID in [1303,2304,2402])')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 五、重复元素处理\n",
    "### 1. duplicated方法\n",
    "#### 该方法返回了是否重复的布尔列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1101    False\n",
       "1102     True\n",
       "1103     True\n",
       "1104     True\n",
       "1105     True\n",
       "dtype: bool"
      ]
     },
     "metadata": {},
     "execution_count": 101
    }
   ],
   "source": [
    "df.duplicated('Class').head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 可选参数keep默认为first，即首次出现设为不重复，若为last，则最后一次设为不重复，若为False，则所有重复项为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "2401     True\n",
       "2402     True\n",
       "2403     True\n",
       "2404     True\n",
       "2405    False\n",
       "dtype: bool"
      ]
     },
     "metadata": {},
     "execution_count": 102
    }
   ],
   "source": [
    "df.duplicated('Class',keep='last').tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "ID\n",
       "1101    True\n",
       "1102    True\n",
       "1103    True\n",
       "1104    True\n",
       "1105    True\n",
       "dtype: bool"
      ]
     },
     "metadata": {},
     "execution_count": 103
    }
   ],
   "source": [
    "df.duplicated('Class',keep=False).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. drop_duplicates方法\n",
    "#### 从名字上看出为剔除重复项，这在后面章节中的分组操作中可能是有用的，例如需要保留每组的第一个值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1201    S_1   C_2      M  street_5     188      68  97.0      A-\n",
       "1301    S_1   C_3      M  street_4     161      68  31.5      B+\n",
       "2401    S_2   C_4      F  street_2     192      62  45.3       A"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_5</td>\n      <td>188</td>\n      <td>68</td>\n      <td>97.0</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2401</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>62</td>\n      <td>45.3</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 104
    }
   ],
   "source": [
    "df.drop_duplicates('Class')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 参数与duplicate函数类似："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "2105    S_2   C_1      M  street_4     170      81  34.2       A\n",
       "2205    S_2   C_2      F  street_7     183      76  85.4       B\n",
       "2305    S_2   C_3      M  street_4     187      73  48.9       B\n",
       "2405    S_2   C_4      F  street_6     193      54  47.6       B"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2105</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>170</td>\n      <td>81</td>\n      <td>34.2</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>2205</th>\n      <td>S_2</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_7</td>\n      <td>183</td>\n      <td>76</td>\n      <td>85.4</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2305</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>187</td>\n      <td>73</td>\n      <td>48.9</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2405</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>193</td>\n      <td>54</td>\n      <td>47.6</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 105
    }
   ],
   "source": [
    "df.drop_duplicates('Class',keep='last')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 在传入多列时等价于将多列共同视作一个多级索引，比较重复项："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1201    S_1   C_2      M  street_5     188      68  97.0      A-\n",
       "1301    S_1   C_3      M  street_4     161      68  31.5      B+\n",
       "2101    S_2   C_1      M  street_7     174      84  83.3       C\n",
       "2201    S_2   C_2      M  street_5     193     100  39.1       B\n",
       "2301    S_2   C_3      F  street_4     157      78  72.3      B+\n",
       "2401    S_2   C_4      F  street_2     192      62  45.3       A"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1101</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_1</td>\n      <td>173</td>\n      <td>63</td>\n      <td>34.0</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>1201</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_5</td>\n      <td>188</td>\n      <td>68</td>\n      <td>97.0</td>\n      <td>A-</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2101</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>174</td>\n      <td>84</td>\n      <td>83.3</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>2201</th>\n      <td>S_2</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_5</td>\n      <td>193</td>\n      <td>100</td>\n      <td>39.1</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2301</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>F</td>\n      <td>street_4</td>\n      <td>157</td>\n      <td>78</td>\n      <td>72.3</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2401</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>62</td>\n      <td>45.3</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 106
    }
   ],
   "source": [
    "df.drop_duplicates(['School','Class'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 六、抽样函数\n",
    "#### 这里的抽样函数指的就是sample函数\n",
    "df.sample()的使用  https://zhuanlan.zhihu.com/p/140665383\n",
    "#### （a）n为样本量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "2305    S_2   C_3      M  street_4     187      73  48.9       B\n",
       "2101    S_2   C_1      M  street_7     174      84  83.3       C\n",
       "2203    S_2   C_2      M  street_4     155      91  73.8      A+\n",
       "2401    S_2   C_4      F  street_2     192      62  45.3       A\n",
       "2202    S_2   C_2      F  street_7     194      77  68.5      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2305</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>187</td>\n      <td>73</td>\n      <td>48.9</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2101</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>174</td>\n      <td>84</td>\n      <td>83.3</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>2203</th>\n      <td>S_2</td>\n      <td>C_2</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>155</td>\n      <td>91</td>\n      <td>73.8</td>\n      <td>A+</td>\n    </tr>\n    <tr>\n      <th>2401</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>62</td>\n      <td>45.3</td>\n      <td>A</td>\n    </tr>\n    <tr>\n      <th>2202</th>\n      <td>S_2</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_7</td>\n      <td>194</td>\n      <td>77</td>\n      <td>68.5</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 107
    }
   ],
   "source": [
    "df.sample(n=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （b）frac为抽样比\n",
    "按百份比抽样."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1205    S_1   C_2      F  street_6     167      63  68.4      B-\n",
       "2401    S_2   C_4      F  street_2     192      62  45.3       A"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1205</th>\n      <td>S_1</td>\n      <td>C_2</td>\n      <td>F</td>\n      <td>street_6</td>\n      <td>167</td>\n      <td>63</td>\n      <td>68.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>2401</th>\n      <td>S_2</td>\n      <td>C_4</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>192</td>\n      <td>62</td>\n      <td>45.3</td>\n      <td>A</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 108
    }
   ],
   "source": [
    "df.sample(frac=0.05)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （c）replace为是否放回   ???"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "2101    S_2   C_1      M  street_7     174      84  83.3       C\n",
       "1301    S_1   C_3      M  street_4     161      68  31.5      B+\n",
       "2303    S_2   C_3      F  street_7     190      99  65.9       C\n",
       "2104    S_2   C_1      F  street_5     159      97  72.2      B+"
      ],
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>School</th>\n      <th>Class</th>\n      <th>Gender</th>\n      <th>Address</th>\n      <th>Height</th>\n      <th>Weight</th>\n      <th>Math</th>\n      <th>Physics</th>\n    </tr>\n    <tr>\n      <th>ID</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1104</th>\n      <td>S_1</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_2</td>\n      <td>167</td>\n      <td>81</td>\n      <td>80.4</td>\n      <td>B-</td>\n    </tr>\n    <tr>\n      <th>2101</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>M</td>\n      <td>street_7</td>\n      <td>174</td>\n      <td>84</td>\n      <td>83.3</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>1301</th>\n      <td>S_1</td>\n      <td>C_3</td>\n      <td>M</td>\n      <td>street_4</td>\n      <td>161</td>\n      <td>68</td>\n      <td>31.5</td>\n      <td>B+</td>\n    </tr>\n    <tr>\n      <th>2303</th>\n      <td>S_2</td>\n      <td>C_3</td>\n      <td>F</td>\n      <td>street_7</td>\n      <td>190</td>\n      <td>99</td>\n      <td>65.9</td>\n      <td>C</td>\n    </tr>\n    <tr>\n      <th>2104</th>\n      <td>S_2</td>\n      <td>C_1</td>\n      <td>F</td>\n      <td>street_5</td>\n      <td>159</td>\n      <td>97</td>\n      <td>72.2</td>\n      <td>B+</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "execution_count": 123
    }
   ],
   "source": [
    "df.sample(n=df.shape[0],replace=True).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sample(n=35,replace=True).index.is_unique"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （d）axis为抽样维度，默认为0，即抽行\n",
    "axis=0 随机抽取n行,axis=1 随机抽取n列."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>street_1</td>\n",
       "      <td>173</td>\n",
       "      <td>34.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>street_2</td>\n",
       "      <td>192</td>\n",
       "      <td>32.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1103</th>\n",
       "      <td>street_2</td>\n",
       "      <td>186</td>\n",
       "      <td>87.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>street_2</td>\n",
       "      <td>167</td>\n",
       "      <td>80.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>street_4</td>\n",
       "      <td>159</td>\n",
       "      <td>84.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Address  Height  Math\n",
       "ID                          \n",
       "1101  street_1     173  34.0\n",
       "1102  street_2     192  32.5\n",
       "1103  street_2     186  87.2\n",
       "1104  street_2     167  80.4\n",
       "1105  street_4     159  84.8"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sample(n=3,axis=1).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （e）weights为样本权重，自动归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Class</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_1</td>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "      <td>34.0</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2402</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_4</td>\n",
       "      <td>M</td>\n",
       "      <td>street_7</td>\n",
       "      <td>166</td>\n",
       "      <td>82</td>\n",
       "      <td>48.7</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1201</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_2</td>\n",
       "      <td>M</td>\n",
       "      <td>street_5</td>\n",
       "      <td>188</td>\n",
       "      <td>68</td>\n",
       "      <td>97.0</td>\n",
       "      <td>A-</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "2402    S_2   C_4      M  street_7     166      82  48.7       B\n",
       "1201    S_1   C_2      M  street_5     188      68  97.0      A-"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sample(n=3,weights=np.random.rand(df.shape[0])).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Class</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2404</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_4</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>160</td>\n",
       "      <td>84</td>\n",
       "      <td>67.7</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2304</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_3</td>\n",
       "      <td>F</td>\n",
       "      <td>street_6</td>\n",
       "      <td>164</td>\n",
       "      <td>81</td>\n",
       "      <td>95.5</td>\n",
       "      <td>A-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_1</td>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "      <td>34.0</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "2404    S_2   C_4      F  street_2     160      84  67.7       B\n",
       "2304    S_2   C_3      F  street_6     164      81  95.5      A-\n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#以某一列为权重，这在抽样理论中很常见\n",
    "#抽到的概率与Math数值成正比\n",
    "df.sample(n=3,weights=df['Math']).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 七、问题与练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 问题\n",
    "#### 【问题一】 如何更改列或行的顺序？如何交换奇偶行（列）的顺序？\n",
    "#### 【问题二】 如果要选出DataFrame的某个子集，请给出尽可能多的方法实现。\n",
    "    问1,2 https://blog.csdn.net/water19111213/article/details/105729828\n",
    "#### 【问题三】 query函数比其他索引方法的速度更慢吗？在什么场合使用什么索引最高效？\n",
    "#### 【问题四】 单级索引能使用Slice对象吗？能的话怎么使用，请给出一个例子。\n",
    "#### 【问题五】 如何快速找出某一列的缺失值所在索引？\n",
    "#### 【问题六】 索引设定中的所有方法分别适用于哪些场合？怎么直接把某个DataFrame的索引换成任意给定同长度的索引？\n",
    "#### 【问题七】 多级索引有什么适用场合？\n",
    "#### 【问题八】 对于多层索引，怎么对内层进行条件筛选？\n",
    "#### 【问题九】 什么时候需要重复元素处理？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 练习\n",
    "#### 【练习一】 现有一份关于UFO的数据集，请解决下列问题："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>shape</th>\n",
       "      <th>duration (seconds)</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10/10/1949 20:30</td>\n",
       "      <td>cylinder</td>\n",
       "      <td>2700.0</td>\n",
       "      <td>29.883056</td>\n",
       "      <td>-97.941111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10/10/1949 21:00</td>\n",
       "      <td>light</td>\n",
       "      <td>7200.0</td>\n",
       "      <td>29.384210</td>\n",
       "      <td>-98.581082</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10/10/1955 17:00</td>\n",
       "      <td>circle</td>\n",
       "      <td>20.0</td>\n",
       "      <td>53.200000</td>\n",
       "      <td>-2.916667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10/10/1956 21:00</td>\n",
       "      <td>circle</td>\n",
       "      <td>20.0</td>\n",
       "      <td>28.978333</td>\n",
       "      <td>-96.645833</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10/10/1960 20:00</td>\n",
       "      <td>light</td>\n",
       "      <td>900.0</td>\n",
       "      <td>21.418056</td>\n",
       "      <td>-157.803611</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           datetime     shape  duration (seconds)   latitude   longitude\n",
       "0  10/10/1949 20:30  cylinder              2700.0  29.883056  -97.941111\n",
       "1  10/10/1949 21:00     light              7200.0  29.384210  -98.581082\n",
       "2  10/10/1955 17:00    circle                20.0  53.200000   -2.916667\n",
       "3  10/10/1956 21:00    circle                20.0  28.978333  -96.645833\n",
       "4  10/10/1960 20:00     light               900.0  21.418056 -157.803611"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('data/UFO.csv').head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （a）在所有被观测时间超过60s的时间中，哪个形状最多？\n",
    "#### （b）对经纬度进行划分：-180°至180°以30°为一个经度划分，-90°至90°以18°为一个维度划分，请问哪个区域中报告的UFO事件数量最多？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 【练习二】 现有一份关于口袋妖怪的数据集，请解决下列问题："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>Name</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>Total</th>\n",
       "      <th>HP</th>\n",
       "      <th>Attack</th>\n",
       "      <th>Defense</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>Speed</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>318</td>\n",
       "      <td>45</td>\n",
       "      <td>49</td>\n",
       "      <td>49</td>\n",
       "      <td>65</td>\n",
       "      <td>65</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>405</td>\n",
       "      <td>60</td>\n",
       "      <td>62</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>60</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>525</td>\n",
       "      <td>80</td>\n",
       "      <td>82</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>VenusaurMega Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>625</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>123</td>\n",
       "      <td>122</td>\n",
       "      <td>120</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>309</td>\n",
       "      <td>39</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   #                   Name Type 1  Type 2  Total  HP  Attack  Defense  \\\n",
       "0  1              Bulbasaur  Grass  Poison    318  45      49       49   \n",
       "1  2                Ivysaur  Grass  Poison    405  60      62       63   \n",
       "2  3               Venusaur  Grass  Poison    525  80      82       83   \n",
       "3  3  VenusaurMega Venusaur  Grass  Poison    625  80     100      123   \n",
       "4  4             Charmander   Fire     NaN    309  39      52       43   \n",
       "\n",
       "   Sp. Atk  Sp. Def  Speed  Generation  Legendary  \n",
       "0       65       65     45           1      False  \n",
       "1       80       80     60           1      False  \n",
       "2      100      100     80           1      False  \n",
       "3      122      120     80           1      False  \n",
       "4       60       50     65           1      False  "
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('data/Pokemon.csv').head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （a）双属性的Pokemon占总体比例的多少？\n",
    "#### （b）在所有种族值（Total）不小于580的Pokemon中，非神兽（Legendary=False）的比例为多少？\n",
    "#### （c）在第一属性为格斗系（Fighting）的Pokemon中，物攻排名前三高的是哪些？\n",
    "#### （d）请问六项种族指标（HP、物攻、特攻、物防、特防、速度）极差的均值最大的是哪个属性（只考虑第一属性，且均值是对属性而言）？\n",
    "#### （e）哪个属性（只考虑第一属性）神兽占总Pokemon的比例最高？该属性神兽的种族值也是最高的吗？"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python370jvsc74a57bd0af09bc94d41e018aa4bb791c06386d7d2a0d085b02fa573368449120a8cb3c6e",
   "display_name": "Python 3.7.0 32-bit (conda)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}